package com.admin.controller;

import cn.hutool.core.io.FileUtil;
import com.admin.entity.Admin;
import com.admin.entity.CopyRight;
import com.admin.service.ICopyRightService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/admin/copyright")
public class CopyRightController {
    @Autowired
    private ICopyRightService iCopyRightService;
    @Value("${upload.tmp.dir}")
    private String tmp;
    @Value("${upload.dir}")
    private String upload;

    @Autowired
    private HttpServletRequest request;

    /**
     * 根据版权id修改版权的信息
     *
     * @param copyRight
     * @return
     */
    @PostMapping("/editCopyRight")
    public ResponseBean editCopyright(CopyRight copyRight) {
        CopyRight copyRight2 =iCopyRightService.getById(copyRight.getCopyrightid());
        String picture = copyRight.getCopyrightpicture();
        if (picture==null || picture.length()==0)
        {
            copyRight.setCopyrightpicture(copyRight2.getCopyrightpicture());
        }
        boolean f = iCopyRightService.updateById(copyRight);
        int code = f ? 200 : 500;
        if(code==200 && copyRight.getCopyrightpicture()!=null && copyRight.getCopyrightpicture().length()>0){
            FileUtil.copy(tmp+"/"+copyRight.getCopyrightpicture(),upload+"/"+copyRight.getCopyrightpicture(),true);
        }
        return new ResponseBean(code, 0, 0);
    }

    /**
     * 新增版权类型
     *
     * @param copyRight
     * @return
     */
    @PostMapping("/addCopyRight")
    public ResponseBean addCopyright(CopyRight copyRight) {
        if (copyRight == null) {
            return new ResponseBean(500, null, "不能为空");
        }
        boolean f = iCopyRightService.save(copyRight);
        int code = f ? 200 : 500;
        if(code==200){
            FileUtil.copy(tmp+"/"+copyRight.getCopyrightpicture(),upload+"/"+copyRight.getCopyrightpicture(),true);
        }
        return new ResponseBean(code, 0, 0);
    }

    /**
     * 查所有版权
     *
     * @param page
     * @param limit
     * @param leibie
     * @return
     */
    @RequestMapping("/selcopyright")
    public ResponseBean selcopyritght(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String leibie) {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null)
        {
            return new ResponseBean(401, "没有登录", null);
        }
        IPage re = iCopyRightService.selCopyRight(page, limit, leibie);
        return new ResponseBean(0, re.getTotal(), re.getRecords());
    }

}
